<template>
  <el-dialog v-model="visible" title="商品分类信息" destroy-on-close width="60%">
    <el-form :model="props.form" label-width="80px" style="padding: 20px 30px 10px 0">
      <el-form-item label="商品图片" >
        <el-upload
            action="http://localhost:9999/files/upload"
            :headers="{token: data.user.token}"
            :on-success="handleFileSuccess"
            list-type="picture"
            v-model="props.form.img"
        >
          <el-button type="primary">上传头像</el-button>
        </el-upload>
      </el-form-item>
      <el-form-item prop="fenlei" label="商品分类" >
        <el-select v-model="props.form.fenlei" placeholder="商品分类" style="width: 240px">
          <el-option
              v-for="item in data.fenlei"
              :key="item.adminId"
              :label="item.fenlei"
              :value="item.fenlei"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="商品重量">
          <el-input v-model.number="props.form.zhongliang" style="width: 200px;">
            <template #append>kg</template>
          </el-input>
      </el-form-item>
      <el-form-item prop="name" label="商品名称" >
        <el-input v-model="props.form.name" autocomplete="off" placeholder="请输入商品名称"/>
      </el-form-item>
      <el-form-item prop="shuliang" label="剩余库存" >
        <el-input v-model="props.form.shuliang" autocomplete="off" placeholder="请输入剩余库存"/>
      </el-form-item>
      <el-form-item prop="jiage" label="商品价格" >
        <el-input v-model="props.form.jiage" autocomplete="off" placeholder="请输入商品价格"/>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="props.customProp = false">取消</el-button>
        <el-button type="primary" @click="save">
          保存
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script setup>
import { computed, reactive } from 'vue';
import { defineProps, defineEmits } from 'vue';
import request from "@/utils/request.js";

const data = reactive({
  user: JSON.parse(localStorage.getItem('code_user') || '{}'),
  fenlei: [],
  pageNum: 1,
  pageSize: 5,
  total: 6,
});

const fenleishuju = () => {
  request.get('shangpinfenlei/selectPage', {
    params: {
      pageNum: data.pageNum,
      pageSize: data.pageSize,
      name: data.name
    }
  }).then(res => {
    data.fenlei = res.data.list;
  }).catch(err => {
    ElMessage.error(err);
  });
};

fenleishuju();

// 👇 正确声明 props
const props = defineProps({
  modelValue: {
    type: Boolean,
    required: true
  },
  form: {
    type: Object,
    required: true
  }
});

const emit = defineEmits(['update:modelValue', 'save']);

const handleFileSuccess = (res) => {
  props.form.img = res.data;
};

const visible = computed({
  get: () => props.modelValue,
  set: (value) => emit('update:modelValue', value)
});

// ✅ 使用 props.form 替代 form
const save = () => {
  try {
    console.log('当前表单数据:', props.form); // 修改这里为 props.form
    if (props.form.id) {
      update();
    } else {
      add();
    }
  } catch (error) {
    ElMessage.error('保存时发生错误');
    console.error(error);
  }
};

const add = () => {
  request.post('/shangpinxiangqing/add', props.form).then(res => {
    if (res.code === '200') {
      emit('update:modelValue', false);
      emit('save')
      ElMessage.success('新增成功');
    } else {
      ElMessage.error(res.msg);
    }
  });
};

const update = () => {
  request.put('/shangpinxiangqing/update', props.form).then(res => {
    if (res.code === '200') {
      emit('update:modelValue', false);
      emit('save')
      ElMessage.success('更新成功');
    } else {
      ElMessage.error(res.msg);
    }
  });
};
</script>

<style scoped>
</style>